package com.jason.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.jason.system.common.ResultEntity;
import com.jason.system.common.util.EasyPoiUtil;
import com.jason.system.common.util.Pager;
import com.jason.system.entity.Number;
import com.jason.system.entity.Order;
import com.jason.system.service.OrderService;
import com.sun.xml.internal.ws.wsdl.writer.document.ParamType;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jie
 * @version 1.0
 * success come from self-discipline
 * @date 2021/3/28 16:21
 */
@RestController
@RequestMapping("/api/order")
@Api(value = "/order",tags = "派单信息接口")
public class OrderController {

  private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

  @Autowired
  private OrderService orderService;

  @ApiOperation(value = "获取派单",notes = "获取所有派单信息")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "page",value = "页数",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "size",value = "每页个数",required = true,dataType = "Integer")
  })
  @GetMapping("/getAll")
  @ApiResponses({
          @ApiResponse(code = 200,message = "获取所有派单信息成功"),
          @ApiResponse(code = 201,message = "获取所有派单信息失败")
  })
  public JSONObject getAllOrders(@RequestParam("page") Integer page ,@RequestParam("size") Integer size){
    logger.info("==>>>OrderController==>>>进行路径：/api/order/getAll.....");
    logger.info("==>>>OrderController==>>>进行路径：page:{}",page);
    logger.info("==>>>OrderController==>>>进行路径：size:{}",size);
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    Pager<Order> orderPager = orderService.getAllOrderS(page, size);
    logger.info("==>>>orderController中获取orders对象==>>>: {}",orderPager);
    if (orderPager.getList()!=null){
      r.setData(orderPager);
      r.setCode(200);
      r.setMessage("获取所有派单信息成功");
    }else {
      r.setCode(201);
      r.setMessage("获取所有派单信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @GetMapping("/getOrderNotFinish")
  @ApiResponses({
          @ApiResponse(code = 200,message = "获取所有未完成派单信息成功"),
          @ApiResponse(code = 201,message = "获取所有未完成派单信息失败")
  })
  public JSONObject getAllOrdersNotFinish(){
    logger.info("==>>>OrderController==>>>进行路径：/api/order/getAllOrderNotFinish.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    List<Order> orderNotFinish = orderService.getAllOrderNotFinish();
    logger.info("==>>>orderController中获取orders对象==>>>: {}",orderNotFinish);
    if (orderNotFinish!=null){
      r.setData(orderNotFinish);
      r.setCode(200);
      r.setMessage("获取所有未完成派单信息成功");
    }else {
      r.setCode(201);
      r.setMessage("获取所有未完成派单信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @PostMapping("/search")
  public JSONObject searchOrders(Integer page , Integer size , String address , String stuName){
    logger.info("==>>>OrderController==>>>进行路径：/api/order/searchOrders.....");
    logger.info("==>>>OrderController==>>>address:{}",address);
    logger.info("==>>>OrderController==>>>stuName:{}",stuName);
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>OrderController==>>>正在查询orders中.....");
    Pager<Order> orders = orderService.searchOrders(page,size, address,stuName);
    logger.info("==>>>OrderController==>>>orders:{}",orders);
    if (orders!=null){
      logger.info("==>>>OrderController==>>>查询orders完成.....");
      r.setCode(200);
      r.setMessage("查询派单信息成功");
      r.setData(orders);
    }else {
      logger.info("==>>>OrderController==>>>查询orders失败.....");
      r.setCode(201);
      r.setMessage("查询派单信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "添加派单",notes = "添加派单信息")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "category",value = "派单类别(电信、校园网、维修平台)",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "address",value = "客户住址",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "appointmentTime",value = "预约时间",required = true,dataType = "Date"),
          @ApiImplicitParam(paramType = "query",name = "number",value = "客户联系方式",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "sendTime",value = "派单时间",required = true,dataType = "Date"),
          @ApiImplicitParam(paramType = "query",name = "description",value = "故障描述",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "dispatcher",value = "派单人",required = true,dataType = "Integer"),
  })
  @ApiResponses({
          @ApiResponse(code = 200,message = "添加派单信息成功"),
          @ApiResponse(code = 201,message = "添加派单信息失败")
  })
  @PostMapping("/insert")
  public JSONObject insertOrder(Order order){
    logger.info("==>>>OrderController==>>>进行路径--order:{}",order);
    logger.info("==>>>OrderController==>>>进行路径：/api/order/insert.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>OrderController==>>>正在添加order中.....");
    try {
      orderService.insertOrder(order);
      logger.info("==>>>OrderController==>>>添加order信息完成.....");
      r.setCode(200);
      r.setMessage("添加派单信息成功");
    }catch (Exception e){
      logger.info("==>>>OrderController==>>>添加order信息失败.....");
      r.setCode(201);
      r.setMessage("添加派单信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "更新派单",notes = "更新派单信息")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "category",value = "派单类别(电信、校园网、维修平台)",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "address",value = "客户住址",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "appointmentTime",value = "预约时间",required = true,dataType = "Date"),
          @ApiImplicitParam(paramType = "query",name = "number",value = "客户联系方式",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "sendTime",value = "派单时间",required = true,dataType = "Date"),
          @ApiImplicitParam(paramType = "query",name = "description",value = "故障描述",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "dispatcher",value = "派单人",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "status",value = "派单状态",required = true,dataType = "Integer")
  })
  @ApiResponses({
          @ApiResponse(code = 200,message = "更新派单信息成功"),
          @ApiResponse(code = 201,message = "更新派单信息失败")
  })
  @PostMapping("/update")
  public JSONObject updateOrder(Order order){
    logger.info("==>>>OrderController==>>>进行路径：/api/order/update.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>OrderController==>>>正在更新order中.....");
    try {
      orderService.updateOrder(order);
      logger.info("==>>>OrderController==>>>更新order信息完成.....");
      r.setCode(200);
      r.setMessage("更新order信息成功");
    }catch (Exception e){
      logger.info("==>>>OrderController==>>>更新order信息失败.....");
      r.setCode(201);
      r.setMessage("更新order信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "更新派单状态",notes = "更新派单信息状态")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "oId",value = "派单id",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "status",value = "派单状态",required = true,dataType = "String")
  })
  @ApiResponses({
          @ApiResponse(code = 200,message = "更新派单信息状态成功"),
          @ApiResponse(code = 201,message = "更新派单信息状态失败")
  })
  @PostMapping("/updateStatus")
  public JSONObject updateOrderStatus(@RequestParam("oId") Integer oId ,@RequestParam("status") Integer status){
    logger.info("==>>>OrderController==>>>进行路径：/api/order/updateStatus.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>OrderController==>>>正在更新order状态中.....");
    try {
      orderService.updateOrderStatus(oId,status);
      logger.info("==>>>OrderController==>>>order状态更新完成.....");
      r.setCode(200);
      r.setMessage("更新order状态成功");
    }catch (Exception e){
      logger.info("==>>>OrderController==>>>order状态更新失败.....");
      r.setCode(201);
      r.setMessage("更新order状态失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "删除派单",notes = "删除派单信息")
  @ApiImplicitParam(paramType = "query",name = "oId",value = "派单id",required = true,dataType = "Integer")
  @ApiResponses({
          @ApiResponse(code = 200,message = "删除派单信息成功"),
          @ApiResponse(code = 201,message = "删除派单信息失败")
  })
  @PostMapping("/delete/{oId}")
  public JSONObject deleteOrder(@PathVariable("oId") Integer oId){
    logger.info("==>>>OrderController==>>>进行路径：oId:{}",oId);
    logger.info("==>>>OrderController==>>>进行路径：/api/order/delete.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>OrderController==>>>正在删除order中.....");
    boolean result = orderService.deleteOrder(oId);
    if (result){
      logger.info("==>>>OrderController==>>>删除order信息完成.....");
      r.setCode(200);
      r.setMessage("删除派单信息成功");
    }else {
      logger.info("==>>>OrderController==>>>删除order信息失败.....");
      r.setCode(201);
      r.setMessage("删除派单信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "批量删除派单",notes = "批量删除派单信息")
  @ApiImplicitParam(paramType = "query",name = "oIds",value = "派单ids",required = true,dataType = "String")
  @ApiResponses({
          @ApiResponse(code = 200,message = "批量删除派单信息成功"),
          @ApiResponse(code = 201,message = "批量删除派单信息失败")
  })
  @PostMapping("/deleteOrders")
  public JSONObject deleteOrders(String oIds){
    logger.info("==>>>OrderController==>>>进行路径：/api/order/deleteOrders.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    //使用流将String字符串转换成list数组
    List<Integer> oIdList = Arrays.stream(oIds.split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
    logger.info("==>>>OrderController==>>>正在批量删除order中.....");
    boolean result = orderService.deleteOrders(oIdList);
    if (result){
      logger.info("==>>>OrderController==>>>批量删除order信息完成.....");
      r.setCode(200);
      r.setMessage("批量删除orders成功");
    }else {
      logger.info("==>>>OrderController==>>>批量删除order信息失败.....");
      r.setCode(201);
      r.setMessage("批量删除orders失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "导出word",notes = "将派单信息导出至word模板中")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "category",value = "派单类别(电信、校园网、维修平台)",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "address",value = "客户住址",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "appointmentTime",value = "预约时间",required = true,dataType = "Date"),
          @ApiImplicitParam(paramType = "query",name = "number",value = "客户联系方式",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "sendTime",value = "派单时间",required = true,dataType = "Date"),
          @ApiImplicitParam(paramType = "query",name = "description",value = "故障描述",required = true,dataType = "String"),
          @ApiImplicitParam(paramType = "query",name = "dispatcher",value = "派单人",required = true,dataType = "Integer"),
  })
  @ApiResponses({
          @ApiResponse(code = 200,message = "导入派单信息至word成功"),
          @ApiResponse(code = 201,message = "导入派单信息至word失败")
  })
  @PostMapping("/downLoad")
  public JSONObject downLoadWord(Order order){
    logger.info("==>>>OrderController==>>>进行路径：/api/order/downLoad.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    Map<String,Object> orderMap = new HashMap<>();
    //将派单信息填充至map集合中
    orderMap.put("category",order.getCategory());
    orderMap.put("address",order.getAddress());
    orderMap.put("appointmentTime",order.getAppointmentTime());
    orderMap.put("number",order.getNumber());
    orderMap.put("sendTime",order.getSendTime());
    orderMap.put("description",order.getDescription());
    orderMap.put("dispatcher",order.getDispatcher());
    logger.info("==>>>OrderController==>>>正在将派单信息导入至word模板中.....");
    try {
      //将信息导入至word模板中，返回下载路径
      String downLoadUrl = EasyPoiUtil.exportWord(orderMap,order.getAddress()+order.getAppointmentTime().toString()+"派单表");
      r.setCode(200);
      r.setMessage("派单信息导入至word成功");
      r.setData(downLoadUrl);
    }catch (Exception e){
      r.setCode(201);
      r.setMessage("派单信息导入至word失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }
}
